package com.qiangou.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.entity.ConTags;
import com.entity.Tags;
import com.qiangou.service.ConTagsService;
import com.qiangou.service.CreditService;
import com.rsa.Base64;
import com.rsa.RSA;
import com.util.DateUtil;

/**
 * @author eastmelon
 */

@Component
@Path("/v1")
public class JerseyController {
	@Resource(name="CTService")
	private ConTagsService CTService;
	
	@Resource(name="creditService")
	private CreditService creditService;

	@Context HttpServletRequest request; 
	
	@GET
	@Path("/getCreditIds")
	@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML})
	public Response getCreditIds(@QueryParam("cardId") String cardId) {
		List<Integer> ids = new ArrayList<Integer>();
		Map<String,Object> data=new HashMap<String,Object>();
		String data1 = String.valueOf(ids);
		data1 = Base64.encode(data1.getBytes());
		String data2 = RSA
				.sign(data1,
						"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKWmOMF5zQBqc8xA41zq9BKHI8GUdZdJR8qP57mv8y2N7T/TD1YeNEx3UH7cSWpsZpdzV5BWKAhH7sTOWfhIU+EhrXrWbQWAndWcT2+ZDYlI0ctJ5Bo1gM0MEU01+g3mhOf70I9DJrYGvoVYV815m+F46bjq8qVEL06zZZLEKCTPAgMBAAECgYEAjZl3rrvFp/NXpWRadtVJaoUm5ZVYp8g2nEtDVJG5mFlYU1TCKWWMY0kjAC6ie1zKnfA1C+b6NYn36zhR5FE/kTSwUYT1P6INT4rD7JUEiwE8hi4MTvWIDCyqeUmb2H+abHpBo9VZymmh5wmwRTi1PgPQGTDq5uP519lgD00DzEECQQDZzHPSvgy0GztmD6Uip1mHDI9j7syIEVCEPtuIsNzH63GQdR5jLH7hZn0WRXEgrZa+f3gKZnFIew+lj6Ip1lPRAkEAwrQrwe6dcioJHdc0PsW/In5TOBTY+ppVKhrkJ6x9UOzZT/BYuXUFVJL8kiKGIK0wihOzMhHK57HjoN5fT5w2nwJBAJ5D4npmXgbWrxAYGFCZOQZYyy28DmZl5pNitdabZqPj5A8r/Bvm7oBOIGF5rp4nZh4htJIiJPmdax5MxHMQarECQH8yMPvqpJT2fSovcwQnL2ybVkZm6DEfLc/p7W81slBxyq38eBoAJtFPjQzy3Ojv+6vYntJw6Ttf7TMk0uMxTEUCQDw1ZXU5jiKoktSYjHjFL2EfqtfT9F8xTHVyaruKL+R67Z0OvxMNlM0j77ADUS/BAFlwF2bCsSkMT3PLvcNtpPk=");
		data.put("data",data1);
		data.put("sign", data2);
		return Response.status(200).entity(String.valueOf(JSONObject.toJSON(data))).build();
	}
	
	@GET
	@Path("/getCreditCount")
	@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML})
	public Response getCreditCount(@QueryParam("types") String type) {
		ConTags ct=new ConTags();
		int count=0;
		Integer userid=(Integer) request.getSession().getAttribute("userid");
		if(userid==null){
			return Response.status(200).entity(String.valueOf("forbidden:You are not logged in")).build();
		}
		if("".equals(type)){
			return Response.status(200).entity(0).build();
		}else{
			type=type.replace("f", "");
		}
		List<String> tags = new ArrayList<String>();
		String[] types = type.split(",");
		tags = creditService.getTagNames(types);
		if(tags.size()>0){
			count = creditService.getCrerditCount(tags);
			
		}
		ct.setCount(count);
		ct.setUserid(userid);
		ct.setTime(DateUtil.getStringDate());
		ct.setTags(type);
		CTService.saveConTags(ct);
		return Response.status(200).entity(String.valueOf(count)).build();
	}
	
	@GET
	@Path("/search")
	@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML})
	public Response getSearchResult(@QueryParam("param") String param) {
//		param=HtmlUtils
		param=param.replace("%", "");
		List<Tags> lt=new ArrayList<Tags>();
		if(!"".equals(param)&&param!=null){
			lt=creditService.getTagsBySearch(param);	
		}
		return Response.status(200).entity(lt).build();
	}
}
